#include "wblib.h"
#include "NUC900_spi.h"

#define SPI_BUFFER_SIZE	2048
__align(4096) UINT8 spiFlash_Buf[SPI_BUFFER_SIZE];
__align(4096) UINT8 spiReadBackBuffer[SPI_BUFFER_SIZE];


int diag_spi()
{
	int status;
	int volatile i;

	sysprintf("\nTest SPI flash ...\n");
#if defined(NUC951)
	sysprintf("H/W Setting: Switch13 ON. Switch14 OFF\n");
#endif	
	sysprintf("\n\nPress any key to start test...\n");
	sysGetChar();

	spiIoctl(SPI_SET_CLOCK, 50);
	status = spiOpen();
	if (status < 0)
		return status;

	/* build test pattern */
	for (i=0; i<SPI_BUFFER_SIZE; i++)
		spiFlash_Buf[i] = i;

	sysprintf("Write data to SPI flash...\n");
	spiWrite(0, SPI_BUFFER_SIZE, (INT32)spiFlash_Buf);

	sysprintf("Read data from SPI flash\n");
	spiRead(0, SPI_BUFFER_SIZE, (INT32)spiReadBackBuffer);

	sysprintf("Compare ...\n");

	status = 0;
	for (i=0; i<SPI_BUFFER_SIZE; i++)
	{
		if (spiReadBackBuffer[i] != spiFlash_Buf[i])
		{
			sysprintf("error! [%d]->wrong[%x] / correct[%x]\n", i, spiReadBackBuffer[i], spiFlash_Buf[i]);
			status = -1;
			break;
		}
	}

	if (status == 0)
		sysprintf("\n\nTest OK!\n");
	else
		sysprintf("\n\nTest Failed!\n");

	return 0;
}

